<template>
    <!-- 修改资料 -->

    <div>
        <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="部门编号" prop="number">
                        <el-input v-model="form.number"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="部门名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="所属公司" prop="companyId">
                        <el-select v-model="form.companyId" clearable placeholder="请选择：">
                            <el-option v-for="item in configData.companyList" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="上级部门" prop="parentId">
                        <el-tree-select v-model="form.parentId" :data="parentData" :render-after-expand="false"
                            :check-strictly=true />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="联系电话" prop="phone">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="部门分类" prop="deptType">
                        <el-select v-model="form.deptType">
                            <el-option v-for="item in configData.deptTypeList" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="负责人" prop="leader">
                        <sc-table-select v-model="leaderRows" :apiObj="apiObj" :fileName="fileName" :table-width="700"
                            :props="tableProps" @change="change">
                        </sc-table-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="顺序号" prop="itemIndex">
                        <el-input-number v-model="form.itemIndex" :min="0" />
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                    <el-form-item label="状态" prop="status">
                        <el-switch v-model="form.status" size="large" inline-prompt active-value="normal" 
                            inactive-value="disable" />
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="传真" prop="fax">
                        <el-input v-model="form.fax"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <!-- </template> -->
        <!-- <template #footer> -->
        <div class="button-position">
            <el-button @click="updateVisible">取 消</el-button>
            <el-button type="primary" @click="addDept(formRef)">确 定</el-button>
        </div>
        <!-- </template> -->

        <!-- </myDialog> -->
    </div>
</template>


<script setup>
import { reactive, ref, onMounted } from 'vue';
import { ElNotification } from 'element-plus';
import Api from '@/api/model/user'
import sysApi from '@/api/model/system'
const emit = defineEmits(['updatetable'])
const props = defineProps({
    paramsData: {
        type: Object,
        defalut: {}
    }
})



// 添加用户数据的对象
const form = ref({
});
const leaderRows = ref([])
const apiObj = ref(sysApi.select.query)
const fileName = ref('ChooseEmployeeReport')
const tableProps = {
    label: 'name',
    value: 'id',
}
const formRef = ref()
const formRules = reactive({
    number: [
        { required: true, message: "请输入编号", trigger: "blur" },
    ],
    name: [
        { required: true, message: "请输入名称", trigger: "blur" },
    ]
})
onMounted(() => {
    getInit()
    getParent()
})

const updateVisible = () => {
    emit('updatetable');
}

const configData = ref([]);
const getInit = async () => {
    let res = await Api.dept.add.post();
    if (res.code == '200') {
        configData.value = res.configData
        form.value = res.data
        if (props.paramsData) {
            form.value.parentId = props.paramsData.id
        }
    }

}

const parentData = ref([]);
const getParent = async () => {
    let params = {
        fileName: 'DeptTreeReport'
    }
    let res = await Api.dept.DeptTree.post(params);
    parentData.value = res.reportData
}
const change = (val) =>{
    form.value.leaderId = val.id
    form.value.leader = val.name
}
const restAddFormData = () => {
    formRef.value.resetFields();
}

// 上传
const addDept = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            addDeptData();
        }
    })
}

const addDeptData = async () => {
    // console.log(form.name);
    var params = {
        bean: form.value,
    }
    var res = await Api.dept.save.post(params);
    console.log(res);
    if (res.code == 200) {
        restAddFormData();
        ElNotification.success({ title: '', message: '添加成功！', duration: 2000 });
        emit('updatetable', true);
    }
    else {
        ElNotification.error({ title: '添加失败', message: res.msg });
    }
}
</script>

<style scoped></style>

